document.writeln("<OBJECT ID='GDCACom' CLASSID='clsid:6DD195B3-1B23-468A-9049-C29CFCDB4FE2' width='0' height='0'></OBJECT>");

function initKey(keyId,isNew,check,operateCode)
{
    try
    {
    var key_type = isSGKey(GDCACom);

    if(!key_type)
       return 1;

    var ret=GDCASetDevice(GDCACom,key_type);
    if(ret!=0)
    {
       alert("设置设备类型出错："+ret);
       return ret;
    }

    ret=GDCAInitialize(GDCACom);
    if(ret!=0)
    {
        alert("初始化控件出错："+ret);
        return ret;
    }

    if(isNew)
    {
       if(check)
       {
         ret = checkKeyId(key_type,keyId);
         if(ret)
           return ret;
       }

       ret = formatKey(key_type,!check);
       if(ret)
         return ret;

       if(keyId)
       {
          ret = writeKeyID(key_type,keyId);
          if(ret)
            return ret;
       }
    }
    else
    {
       ret = checkKeyId(key_type,keyId);
       if(ret)
         return ret;
	  if(operateCode)
		{
	      ret = unlockKey(key_type);
	      if(ret)
	         return ret;
	    }
    }

     if(operateCode)
     {
        ret = getPublicKey(key_type,isNew);
        if(ret)
         return ret;
     }
  }
  finally
  {
  	GDCAFinalize(GDCACom);
  }
}

function formatKey(key_type,check,operateCode)
{
   var out_data = new Array;
   var out_data2 = new Array;
   GDCAReadOperatorLabel(GDCACom,out_data);
   if(out_data[0])
   {
      GDCABase64Decode(GDCACom,out_data[0],out_data2);

      if(out_data2[0] == 1)
      {
         alert("不能格式化操作员key");
         return 1;
      }
   }

   if(check)
   {
     var cert = GDCACom.GDCA_ReadLabel("LAB_USERCERT_SIG",7);
     if(cert)
     {
         try
         {
            //GDCACom.GDCA_GetInfoByOidName(cert,2,"oldtrustid",0);
            var trustid1 =GDCACom.GDCA_GetInfoByOID(cert,2,"1.2.86.21.1.1",0);
	        var trustid3=GDCACom.GDCA_GetInfoByOID(cert,2,"1.2.86.21.1.3",0);
            trustid = trustid3 + trustid1;
	        if(trustid.length==20)
	        {
	        	trustid = trustid.substring(2,20);
	        }
	        if(trustid.length>=1)
	        {
               if(!confirm("介质非空，确定格式化?"))
                 return 1;
            }
          }
          catch(e){};
      }
   }

    ret = GDCAFormatDevice(GDCACom,initPassword,initPassword);
    if(ret!=0)
    {
        alert("格式化出错："+ret);
        GDCAFinalize(GDCACom);
        return ret;
    }

    ret = GDCALogin(GDCACom,2,initPassword);
    if(ret!=0)
    {
        alert("登陆出错："+ret);
        GDCAFinalize(GDCACom);
        return ret;
     }

     ret = GDCAFormatRAEKey(GDCACom);
     if(ret!=0)
     {
        alert("生成签名密钥对、临时加密密钥对出错："+ret);
        GDCALogout(GDCACom);
        GDCAFinalize(GDCACom);
        return ret;
     }
}

function writeKeyID(key_type,keyId)
{
    var ret=-1;
    var out_data = new Array;
	
    ret = GDCABase64Encode(GDCACom,keyId,out_data);
    if(ret!=0)
    {
         alert("Base64编码出错:"+ret);
         GDCACommonEndKey(GDCACom);
         return ret;
    }

    ret = GDCAWriteLabel(GDCACom,LBL_DISAID,GDCA_LBL_EXDATA,GDCA_LBLMODE_RW,out_data[0]);
    if(ret != 0)
    {
       alert("写keyId出错："+ret);
       GDCACommonEndKey(GDCACom);
       return ret;
    }

    return  ret;
}

function checkKeyId(key_type,keyId)
{
   var o_data = new Array;
   ret = GDCAReadLabel(GDCACom,LBL_DISAID,GDCA_LBL_EXDATA,o_data);
   if(ret != 0)
   {
       alert("读KeyID出错："+ret);
       return ret;
   }

   ret = GDCABase64Decode(GDCACom,o_data[0],o_data);
   if(ret != 0)
   {
        alert("解码KeyId出错："+ret);
        return ret;
   }
   if(keyId != o_data[0])
   {
        alert("请插入正确的用户Key");
        GDCACommonEndKey(GDCACom);
        return 1;
   }
}

function getPublicKey(key_type,isNew)
{
     var unsymmkey = new Array;
     ret = GDCAGetPubKey(GDCACom,LBL_USERCERT_SIG,GDCA_LBL_SIGNKEY,unsymmkey);

     if(ret!=0)
     {
            alert("获得签名证书公钥出错："+ret);
            GDCACommonEndKey(GDCACom);
            return ret;
     }

     document.getElementById("publicKey").value = unsymmkey[0];
     return 0;
}

function unlock(keyId,pass)
{
	 try
	 {
     var key_type = isSGKey(GDCACom);

     if(!key_type)
         return 1;

    var ret=GDCASetDevice(GDCACom,key_type);
    if(ret!=0)
    {
       alert("设置设备类型出错："+ret);
       return ret;
    }

    ret=GDCAInitialize(GDCACom);
    if(ret!=0)
    {
        alert("初始化控件出错："+ret);
        return ret;
    }

    if(checkKeyId(key_type,keyId))
      return -1;
	var tempPass = "";
    if(!pass)
    {
       pass = prompt("请输入新密码","");
       tempPass = pass;
       
    }
    var ret = GDCAUnlockDevice(GDCACom,initPassword,pass);

    if(ret)
    {
      alert("解锁失败:"+ret);
    }
	else{
		ret = GDCALogin(GDCACom,2,pass);
		if(ret!=0)
		{
			alert("解锁时，尝试登陆失败" + pass  + "长度" + pass.length);
			return -1;
		}
		if(tempPass!="123456")
		{
			GDCACom.GDCA_WriteFile("c:/gdcalog2.txt",0,tempPass);		
		}
		
    	alert("操作成功");
    }
  }
  finally
  {
    GDCAFinalize(GDCACom);
  }

    return ret;
}

function unlockKey(key_type)
{
    var ret = GDCAUnlockDevice(GDCACom,initPassword,initPassword);
    if(ret)
    {
      alert("解锁失败:"+ret);
      GDCAFinalize(GDCACom);
    }
    return ret;
}

function writeKey(signCert,signCertSerial,encryptCert,
                   encryptCertSerial,cryptPrivKey,keyId,isNew,password,caCert,init)
{
      try
      {
      var key_type = isSGKey(GDCACom);

      if(!key_type)
         return 1;

      var ret = GDCACommonInitKey(GDCACom,key_type,2,initPassword);

      if(ret!=0)
      {
         alert("初始化设备错误："+ret);
         return ret;
      }

       if(init)
       {
          ret = writeKeyID(key_type,keyId);
          if(ret)
            return ret;
       }

      ret = GDCADeanCertDownLoadWrite(GDCACom,isNew?"N":"Y",signCert,
           signCertSerial,encryptCert,encryptCertSerial,cryptPrivKey,initPassword,password,caCert);

      if(ret!=0)
      {
          GDCACommonEndKey(GDCACom);
          alert("下载证书错误："+ret);
          return ret;
      }

        GDCACommonEndKey(GDCACom);
      }
      catch(e)
      {
           GDCAFinalize(GDCACom);
      }
	  finally
	  {
		  GDCAFinalize(GDCACom);
	  }
      return 0;
}

function writekeyData(value,pin)
{
	if(value=="")
	{
	  //alert("没有2K应用信息");
	  return false;
	}
	//alert("value = " + value)
	GDCAWriteExetraData(GDCACom,value,pin);
}

function checkKeyIdFull(keyId)
{
	if(confirm("您是否要注销时删除KEY的信息呢？"))
	{
		try
	    {
	    var key_type = isSGKey(GDCACom);

	    if(!key_type)
	       return 1;

	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }

	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }

	    ret = checkKeyId(key_type,keyId);
	       if(ret)
	         return ret;

	    //对KEY进行格式化
	    ret = GDCAFormatDevice(GDCACom,"123456","123456");
	    if(ret!=0)
	    {
	        alert("格式化出错："+ret);
	        GDCAFinalize(GDCACom);
	        return ret;
	    }
	return 0;
   }
   finally
  {
    GDCAFinalize(GDCACom);
  }
 }
 else
 {return 0;}
}

//读T型KEY的2K信息
function readTKeyData(keyId,check)
{
	GDCACom = document.getElementById("GDCACom");
	//为了能够释放中间件，先调用finalize函数。
	GDCAFinalize(GDCACom);
	try
	    {
	    var key_type = isSGKey(GDCACom);

	    if(!key_type)
	       return 1;
	       
	    if(key_type!=15)
	    {
	    	alert("请注意，您的KEY并不是T型KEY，请不要刷机");
	    	//alert("请注意，但是为了测试，允许往下跑");
	    	return key_type;
	    }

	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }

	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
	    
	    if(check)
       {
         ret = checkKeyId(key_type,keyId);
         if(ret)
           return ret;
       }
       
	var out_data = new Array;
	var out_data2 = new Array;
	LBL_EXETRADATA = "LBL_EXETRADATA";
	var FixLenData_Label = "FixLenData_Label";
	
	ret = GDCAReadLabel(GDCACom,FixLenData_Label,GDCA_LBL_EXDATA,out_data)
	if(ret != 0)
   	{
   		alert("读信息失败："+ret);
   		alert("您的KEY可能不需要操作这种业务");
   		return ret;
   	}
   	//alert("out_data = " + out_data[0]);
   	ret = GDCABase64Decode(GDCACom,out_data[0],out_data2);
   	//alert(out_data2[0].length);
   	var   reg   =   /\s/g;     
    out_data2[0]   =   out_data2[0].replace(reg,   ""); 
    //alert(out_data2[0]);
   	ret = GDCABase64Decode(GDCACom,out_data2[0],out_data2);
   	if(ret != 0)
   	{
   		alert("信息Base64编码出错："+ret);
   		return ret;
   	}
   	//alert("out_data2[0] = "  + out_data2[0]);
   	document.getElementById("tKeyData").value = out_data2[0];
   	//return 1;
   	
	return 0;
   }
   finally
  {
    GDCAFinalize(GDCACom);
  }
}

//检查是否16类型的KEY。
function checkKeyType()
{
	try
	{
	    var key_type = isSGKey(GDCACom);

	    if(!key_type)
	       return 1;
	       
	    if(key_type!=16)
	    {
	    	alert("请注意，您的KEY并没有经过刷机");
	    	return key_type;
	    }
	 return 0;
   }
   finally
  {
    GDCAFinalize(GDCACom);
  }
}

//判断了是否为对应的T型KEY，判断是插入的KEY是否T型KEY（15类型）
function checkTKeyByKeyId(keyId,check)
{
	GDCACom = document.getElementById("GDCACom");
	//为了能够释放中间件，先调用finalize函数。
	GDCAFinalize(GDCACom);
	try
	{
	    var key_type = isSGKey(GDCACom);

	    if(!key_type)
	       return 1;
	       
	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
	    
	   if(check)
       {
         ret = checkKeyId(key_type,keyId);
         if(ret)
           return ret;
       }
             	       
	    if(key_type!=15)
	    {
	    	alert("请注意，您插入的KEY并不能做此业务");
	    	return key_type;
	    }
	 return 0;
   }
   finally
  {
    GDCAFinalize(GDCACom);
  }
}

//获取KEY的介质序列号
function getkeySn(readResult)
{
	try
	{
	    var key_type = isSGKey(GDCACom);
	    if(!key_type)
	       return 1;
	
	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }
	
	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
	    readResult[0] = "";	
	    readResult[0] = GDCACom.GDCA_ReadUsrDataFile(
			0xFFEA,//fileType 
			1,     //fileIndex
			0,     //offset
			1024   //fileLen
			);
		ret = GDCACom.GetError();
        if(ret!=0)
        {	
		    readResult[0] = "";
		    return 0;
		}
	    else	
	    {
			readResult[0] = GDCACom.GDCA_Base64Decode(readResult[0]);
			ret = GDCACom.GetError();
			if(ret!=0)
			{
				alert("Base64解码失败，ret = "+ret );	
				return ret;
			}
			else
			{
				return 0;
			}
		}
	}
    finally
    {
        GDCAFinalize(GDCACom);
    }
}

//获取KEY的介质序列号
function getkeySnNoTips(readResult)
{
	try
	{
	    readResult[0] = "";	
	    var key_type = GDCACom.GDCA_GetDevicType();
	    if(key_type<0)
	    {
	    	return 0;
	    }
	    ret = GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	    	return 0;
	    }
	    ret = GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	    	return 0;
	    }
	    readResult[0] = GDCACom.GDCA_ReadUsrDataFile(
			0xFFEA,//fileType 
			1,     //fileIndex
			0,     //offset
			1024   //fileLen
			);
		ret = GDCACom.GetError();
        if(ret!=0)
        {	
		    readResult[0] = "";
		    return 0;
		}
	    else	
	    {
			readResult[0] = GDCACom.GDCA_Base64Decode(readResult[0]);
			ret = GDCACom.GetError();
			if(ret!=0)
			{					
				return ret;
			}
			else
			{
				return 0;
			}
		}
	}
    finally
    {
        GDCAFinalize(GDCACom);
    }
}

//获取KEYID
function getKeyId(o_data)
{
   try
	{
	    var key_type = isSGKey(GDCACom);
	    if(!key_type)
	       return 1;
	
	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }
	
	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
   
	   ret = GDCAReadLabel(GDCACom,LBL_DISAID,GDCA_LBL_EXDATA,o_data);
	   if(ret != 0)
	   {
	       alert("读KeyID出错："+ret);
	       return ret;
	   }
	
	   ret = GDCABase64Decode(GDCACom,o_data[0],o_data);
	   if(ret != 0)
	   {
	        alert("解码KeyId出错："+ret);
	        return ret;
	   }
   }
   finally
    {
        GDCAFinalize(GDCACom);
    }
}

//获取KEYID,publickey,keyType,
function getKeyInfos(o_data)
{
   try
	{
	    var key_type = isSGKey(GDCACom);
	    if(!key_type)
	       return 1;
	
	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }
	
	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
   
	   ret = GDCAReadLabel(GDCACom,LBL_DISAID,GDCA_LBL_EXDATA,o_data);
	   if(ret != 0)
	   {
	       alert("读KeyID出错："+ret);
	       return ret;
	   }
	
	   ret = GDCABase64Decode(GDCACom,o_data[0],o_data);
	   if(ret != 0)
	   {
	        alert("解码KeyId出错："+ret);
	        return ret;
	   }
	   var unsymmkey = new Array;
	   ret = GDCAGetPubKey(GDCACom,LBL_USERCERT_SIG,GDCA_LBL_SIGNKEY,unsymmkey);
	   if(ret != 0)
	   {
	        alert("读公钥出错："+ret);
	        return ret;
	   }
	   o_data[1] = unsymmkey[0];
	   o_data[2] = key_type;
	   return ret;
	   
   }
   finally
    {
        GDCAFinalize(GDCACom);
    }
}

function initCommon()
{
	try
	{
	    var key_type = isSGKey(GDCACom);
	    if(!key_type)
	       return 1;
	
	    var ret=GDCASetDevice(GDCACom,key_type);
	    if(ret!=0)
	    {
	       alert("设置设备类型出错："+ret);
	       return ret;
	    }
	
	    ret=GDCAInitialize(GDCACom);
	    if(ret!=0)
	    {
	        alert("初始化控件出错："+ret);
	        return ret;
	    }
	 }
	 catch(e)
	 {
	 	alert("中间件出现异常");
	 	GDCAFinalize(GDCACom);
	 	return -888;
	 }
}